﻿using System.Xml;
using System.Collections;

namespace OrganizationName.Xml
{
    public class XmlExtendedDocument : System.Xml.XmlDocument
    {
        protected bool _ExtensionsAreExecuted;
        protected IDictionary _Extensions;

        public virtual IDictionary Extensions
        {
            get
            {
                return _Extensions;
            }
            set
            {
                _Extensions = value;
            }
        }

        private void ExecuteExtensions()
        {
            if (Extensions != null)
            {
                foreach (IXmlExtendedDocumentExtension extension in Extensions.Values)
                {
                    if (extension != null)
                    {
                        extension.Extend(this);
                    }
                }
            }
        }

        public override void Load(string filename)
        {
            _ExtensionsAreExecuted = false;
            base.Load(filename);
            ExecuteExtensions();
        }

        public override void Load(System.IO.Stream inStream)
        {
            _ExtensionsAreExecuted = false;
            base.Load(inStream);
            ExecuteExtensions();
        }

        public override void Load(System.IO.TextReader txtReader)
        {
            _ExtensionsAreExecuted = false;
            base.Load(txtReader);
            ExecuteExtensions();
        }

        public override void Load(XmlReader reader)
        {
            _ExtensionsAreExecuted = false;
            base.Load(reader);
            ExecuteExtensions();
        }

        public override void LoadXml(string xml)
        {
            _ExtensionsAreExecuted = false;
            base.LoadXml(xml);
            ExecuteExtensions();
        }
    }
}
